$(document).ready(function() {
		$.ajax({  
			type: 'get',
			//async: false,
			dataType: "json",
			url:basePath + 'menu/loadMenu',
			beforeSend: function(request) {
				request.setRequestHeader("header", localStorage.getItem("token"));
			},
			data:{
				parentId:0
			},
			success: function(data){
				$.each(data,function(i,n){
					//get children
					var childrenmenulist = getChildren(n.id);

					 $('#menu').accordion('add',{
							title:function(){return '<img src="'+basePath +'images/menu/'+n.icon+'.png" height="15" width="15"/>&nbsp;&nbsp;'+ n.menuName;},
							selected: false,
							content:childrenmenulist
						});						

				});
			
			}
		}); 
		
	});

function getChildren(parentId){
	var result = "";
	$.ajax({  
		type: 'get',
		async: false,
		dataType: "json",
		url:basePath+'menu/loadChildrenMenu',
		beforeSend: function(request) {
			request.setRequestHeader("header", localStorage.getItem("token"));
		},
		data:{
			parentId:parentId
		},
		success: function(data){
    		var menulist ='';
    		menulist +='<ul>';
            $.each(data, function(j, o) {
    			menulist += '<li onclick = clickMenu("' + o.menuName+'","' + o.url+'");><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a class="menua" >' + o.menuName + '</a></div></li>';
            })
    		menulist += '</ul>';
            result = menulist;
		}
	});
	return result;
}
/*
var titlearr = new Array();
function clickmenu(title,url){
	var flag = $("#tabs").tabs("exists",title);
	titlearr.push(title);
	for(var i = 0; i < titlearr.length; i++){
		$("#tabs").tabs("close",titlearr[i]);
	}
	var content = '<iframe scrolling="auto" frameborder="0"  src="'+url+'" style="width:100%;height:100%;"></iframe>';
	//打开选项卡
	$("#tabs").tabs("add",{
		"title" : title,
		"closable" : true,
		"content" : content,
		//"href" : basePath+url,
		"iconCls" : "icon-add"
	});
}
*/
function clickMenu(title,url){
	var flag = $("#tabs").tabs("exists",title);
	var srcUrl = basePath + url;
	var content = '<iframe scrolling="auto" frameborder="0"  src="'+ url + '" style="width:100%;height:100%;"></iframe>';
	//打开选项卡
	if(!flag){
		$("#tabs").tabs("add",{
			"title" : title,
			"closable" : true,
			"content" : content,
			//"href" : basePath+url,
			"iconCls" : "icon-add"
		});		
	}else{
		$('#tabs').tabs('select', title);
	}

}

